package com.gxa.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.ContractMapper;
import com.gxa.param.MyTableParam;
import com.gxa.pojo.Contract;
import com.gxa.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractMapper contractMapper;


    /**
     * 合同列表
     *
     * @param param
     * @return
     */
    @Override
    public LayR list(MyTableParam param) {
        System.out.println(param);
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Contract> contracts = contractMapper.findAll(param);
        PageInfo<Contract> contractPageInfo = new PageInfo<>(contracts);
        return new LayR(0, "success", contractPageInfo.getList(), contractPageInfo.getTotal());
    }

    /**
     * 添加合同
     *
     * @param contract
     * @return
     */
    @Override
    public R add(Contract contract) {
        contract.setCreateTime(new Timestamp(System.currentTimeMillis()));
        contractMapper.saveContract(contract);
        return new R(200, "添加成功");
    }

    /**
     * 删除一条合同数据
     *
     * @param id
     * @return
     */
    @Override
    public R deleteOne(Integer id) {
        contractMapper.deleteOneById(id);
        return new R(200, "删除成功");
    }

    /**
     * 批量删除合同数据
     *
     * @param ids
     * @return
     */
    @Override
    public R deleteAll(String[] ids) {

        contractMapper.deleteAll(ids);
        return new R(200, "删除成功");
    }

    /**
     * 通过id查询合同信息
     *
     * @param id
     * @return
     */
    @Override
    public R findById(Integer id) {
        Contract contractMapperByPk = contractMapper.findByPk(id);
        return new R(200, "success", contractMapperByPk);
    }

    /**
     * 通过id查询合同详细信息，并能显示出来
     *
     * @param id
     * @return
     */
    @Override
    public LayR queryById(Integer id) {
        List<Contract> contractList = new ArrayList<>();
        contractList.add(contractMapper.findByPk(id));
        LayR layR = new LayR();
        layR.setCode(0);
        layR.setMsg("success");
        layR.setData(contractList);
        layR.setCount((long)1);
        return layR;
    }

    /**
     * 修改合同
     *
     * @param contract
     * @return
     */
    @Override
    public R edit(Contract contract) {
        contract.setLastOperationTime(new Timestamp(System.currentTimeMillis()));
        contractMapper.updateContract(contract);

        return new R(200, "修改成功");
    }
}
